﻿<!DOCTYPE html>
@{
    string selectQueryString = "";
    string selected = "";
    string from = "";

    from = Request.Form["from"];
    selected = Request.Form["selected"];
    
    string connString =  System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connString);

    switch(from)
    {
        case "hosts":
        {
            string name, hardware_ethernet, fixed_address, subnet;

            name = Request.Form["name"];
            hardware_ethernet = Request.Form["hardware_ethernet"];
            fixed_address = Request.Form["fixed_address"];
            subnet = Request.Form["subnet"];

            selectQueryString = "UPDATE hosts SET name='"+name+"', hardware_ethernet='"+hardware_ethernet+"', fixed_address='"+fixed_address+"', subnet="+subnet+" WHERE name='"+selected+"';";
            break;
        }
        case "subnets":
        {
            string address, netmask, _range, broadcast_address, routers, server, building, name;

            address = Request.Form["address"];
            netmask = Request.Form["netmask"];
            _range = Request.Form["_range"];
            broadcast_address = Request.Form["broadcast_address"];
            routers = Request.Form["routers"];
            server = Request.Form["server"];
            building = Request.Form["building"];
            name = Request.Form["name"];

            selectQueryString = "UPDATE subnets SET address='"+address+"', netmask='"+netmask+"', _range='"+_range+"', broadcast_address='"+broadcast_address+"', routers='"+routers+"', server="+server+", building='"+building+"', name='"+name+"' WHERE name='"+selected+"';";
            break;
        }
        case "servers":
        {
            string domain_name_servers, default_lease_time, max_lease_time, authoritative, name, log_facility;

            domain_name_servers = Request.Form["domain_name_servers"];
            default_lease_time = Request.Form["default_lease_time"];
            max_lease_time = Request.Form["max_lease_time"];
            authoritative = Request.Form["authoritative"];
            name = Request.Form["name"];
            log_facility = Request.Form["log_facility"];

            selectQueryString = "UPDATE servers SET domain_name_servers='"+domain_name_servers+"', default_lease_time="+default_lease_time+", max_lease_time="+max_lease_time+", authoritative="+authoritative+", name='"+name+"', log_facility='"+log_facility+"' WHERE name='"+selected+"';";
            break;
        }
    }

    try
    {
        MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand(selectQueryString, connection);
        connection.Open();
        command.ExecuteNonQuery();

        switch(from)
        {
            case "hosts":
            {
                Response.Redirect("hosts.cshtml");
                break;
            }
            case "subnets":
            {
                Response.Redirect("subnets.cshtml");
                break;
            }
            case "servers":
            {
                Response.Redirect("default.cshtml");
                break;
            }
        }
    }
    catch(Exception ex)
    {
        <html lang="en">
            <head>
                <meta charset="utf-8" />
                <title>Layout</title>
                <link href="Images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
                <link rel="stylesheet" type="text/css" href="Shared/styles.css">
            </head>
            <body>
                <div style="background-color: #fff; width: 500px; height: auto; margin-left: auto; margin-right: auto; margin-top: 400px">
                    <p style="color: #f00; margin-left: 10px; margin-right: 10px; padding-top: 10px; font-family: 'Segoe UI'; font-size: medium; text-align: justify"><strong>Sprawdź wprowadzone dane!</strong></p>

                    <p style="margin-left: 10px; margin-right: 10px; font-family: 'Segoe UI'; font-size: medium; text-align: justify">@ex.Message</p>
                    @{
                        switch(from)
                        {
                            case "servers":
                            {
                                <button onclick="navigate('default.cshtml')" style="width: 100px; margin-left: 200px; margin-bottom: 10px; margin-top: 20px; position: relative">OK</button>
                                break;
                            }
                            case "hosts":
                            {
                                <button onclick="navigate('Hosts.cshtml')" style="width: 100px; margin-left: 200px; margin-bottom: 10px; margin-top: 20px; position: relative">OK</button>
                                break;
                            }
                            case "subnets":
                            {
                                <button onclick="navigate('Subnets.cshtml')" style="width: 100px; margin-left: 200px; margin-bottom: 10px; margin-top: 20px; position: relative">OK</button>
                                break;
                            }
                        }
                    }

                </div>
            </body>
        </html>
    }
    finally
    {
        connection.Close();
    }
}